// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

const {ccclass, property} = cc._decorator;

enum SceneStatus {Idling,Prepare,Throwing,Waiting,Fighting,Ending};
enum ThrowDirction {FORWARD,BACKWARD};
import catctl = require("../objctl/catctl");
import fishctl = require("../objctl/fishctl");
import FishMgr from "./fishmgr";

@ccclass
export default class FishSceneMgr extends cc.Component {

    @property(cc.Label)
    label: cc.Label = null;

    @property(cc.Button)
    funcBtn: cc.Button = null;

    @property(cc.Slider)
    throwSlider: cc.Slider = null;

    @property(cc.Node)
    river:cc.Node = null

    @property(cc.Prefab)
    fishPrefab:cc.Prefab

    @property(catctl.default)
    catObj: catctl.default = null

    private fishMgr: FishMgr = null;
    private fishObj: fishctl.FishController= null;
    private waitingTime:number;//等待剩余时间
    private fightingTime:number;
    private btnText:cc.Label

    sceneStatus: SceneStatus
    dirction: ThrowDirction
    handlerSped: number = 0.02
    start () 
    {
        this.btnText = cc.find("/Background/Label",this.funcBtn.node).getComponent(cc.Label)
        this.fishMgr = new FishMgr()
        this.changeSceneStatus(SceneStatus.Idling)
    }
    //--------------清理数据--------------
    private reset(){
        this.waitingTime = 0
        this.fightingTime = 0
        this.fishObj = null
        this.sceneStatus = SceneStatus.Idling
        this.dirction = ThrowDirction.FORWARD
    }
    //--------------控制界面显示--------------
    showConfirmButton()
    {
        this.btnText.string="抛"
    }
    showPullButton()
    {
        this.btnText.string="收线"
    }

    showPrepareButton()
    {
        this.btnText.string="准备"
    }
    hideFuncButton()
    {
        this.funcBtn.node.active = false
    }
    showFuncButton()
    {
        this.funcBtn.node.active = true
    }

    showThrowSlider()
    {
        this.throwSlider.node.active = true
    }
    hideThrowSLider()
    {
        this.throwSlider.node.active = false
    }

    setIdlingScene()
    {
        this.showPrepareButton()
        this.hideThrowSLider()
    }

    setFishingScene()
    {
        this.showPullButton()
        this.hideThrowSLider()
    }

    setPrepareScene()
    {
        this.showConfirmButton()
        this.showThrowSlider()
    }
    
    setThrowingScene(){
        this.hideThrowSLider()
    }

    setFightingScene(){
        this.showPullButton()
        this.hideThrowSLider()
    }
    //--------------控制界面显示--------------
    updateUI(){
        switch (this.sceneStatus) {
            case SceneStatus.Idling:
                this.setIdlingScene()
                break;
            case SceneStatus.Prepare:
                this.setPrepareScene()
                break;
            case SceneStatus.Throwing:
                this.hideFuncButton()
                this.setThrowingScene()
                break;
            case SceneStatus.Waiting:
                this.showFuncButton()
                this.setFishingScene()
                break;
            case SceneStatus.Fighting:
                this.setFishingScene()
                break;
            case SceneStatus.Ending:
                this.setIdlingScene()
            default:
                break;
        }
    }

    updateCatLogic(){
        switch (this.sceneStatus) {
            case SceneStatus.Idling:
                //暂时并没有待机动画
                this.catObj.PlayAnimation("idle")
                break;
            case SceneStatus.Prepare:
                //也没有准备动画
                this.catObj.PlayAnimation("prepare")
                break;
            case SceneStatus.Throwing:
                this.catObj.PlayThrowingAnimation()
                break;
            case SceneStatus.Waiting:
                this.catObj.PlayWaitAnimation()
                break;
            case SceneStatus.Fighting:
                //暂时没有动画
                this.catObj.PlayAnimation("fighting")
                break;
            case SceneStatus.Ending:
                this.catObj.PlayBackRobAnimation()
            default:
                break;
        }
    }

    updateFishLogic(){

    }

    changeSceneStatus(newStatus:SceneStatus):void{
        if(newStatus == this.sceneStatus)
        {
            return
        }
        cc.log("change scene status from "+this.sceneStatus+" to "+newStatus)
        this.sceneStatus = newStatus

        switch (this.sceneStatus) {
            case SceneStatus.Idling:
                this.reset()
                let cur = this.fishMgr.FishNum()
                if (cur < 5) {
                    let needNum = 5 - cur;
                    this.fishMgr.GenerateFish(needNum, this.river, this.fishPrefab)
                }
                break
            case SceneStatus.Ending:
                if(this.fishObj != null)
                {
                    this.fishObj.node.removeFromParent(true)   
                }
        }

        this.updateUI()
        this.updateCatLogic()
        this.updateFishLogic()        
    }
    
    update (dt) 
    {
        switch(this.sceneStatus)
        {
            case SceneStatus.Prepare:
                this.swing_throw_handler()
                break;
            case SceneStatus.Throwing:
                let isFinish = this.catObj.IsAnimationEnd("waverob")
                if(isFinish){
                    this.changeSceneStatus(SceneStatus.Waiting)
                }
                break
            case SceneStatus.Waiting:
                this.waitingTime += dt
                if(this.waitingTime > 10){
                    //时间耗尽
                    console.log("等待时间耗尽")
                    this.changeSceneStatus(SceneStatus.Ending)
                    break
                }
                
                if(this.fishObj == null)
                {//还没鱼上钩
                    //每两秒随机一次
                    if (Math.floor(this.waitingTime % 2) > 0)
                    {
                        break
                    }
                    console.log("鱼还没上钩随机一次")
                    this.fishObj = this.fishMgr.CatchFish()
                    if (this.fishObj != null) {
                        console.log("随机到一条鱼")
                        this.fishObj.BeAttract(this.catObj.GetBaitNode())
                    }
                    else {
                        //还没鱼上钩
                        console.log("还是没有随机到鱼,继续等待")
                        break
                    }
                } 
                let baitBox = this.catObj.GetBaitWorldBox()
                let fishBox = this.fishObj.GetWorldBox()
                if (fishBox.containsRect(baitBox))
                {
                    console.log("鱼上钩了,进入下一个状态")
                    this.catObj.PlayAnimation("warn")
                    //鱼开始逃跑
                    this.fishObj.RunAway()
                    //鱼上钩了,进入下一个状态
                    this.changeSceneStatus(SceneStatus.Fighting)
                }
                break;
            case SceneStatus.Fighting:
                this.fightingTime += dt
                if(this.fightingTime > 10){
                    this.changeSceneStatus(SceneStatus.Ending)
                    break
                }
                if(this.fishObj == null){
                    //这里有问题
                    console.log("SceneStatus.Fighting fishObj == null")
                    break
                }
                if(this.catObj.IsRobBroken()){
                    console.log("用力过猛,线断了")
                    this.changeSceneStatus(SceneStatus.Ending)
                }

                if(this.fishObj.IsGiveUp()){
                    this.fishObj.GiveUp(this.catObj.GetBaitNode())
                    this.changeSceneStatus(SceneStatus.Ending)
                }
                break
            case SceneStatus.Ending:
                if(this.catObj.IsAnimationEnd("backrobnothing"))
                {//动画播完
                    this.Calculate()
                    this.changeSceneStatus(SceneStatus.Idling)
                }
                break
        }
        if(this.fishObj != null)
        {
            this.fishObj.DoUpdate(dt)
        }
        this.fishMgr.DoUpdate(dt)
    }

    //结算
    public Calculate() {
        let tipNode = new cc.Node()
        let tipLabel = tipNode.addComponent<cc.Label>(cc.Label)
        if(this.fishObj == null)
        {
            tipLabel.string = "没有鱼上钩"
        }
        if(this.fishObj.IsGiveUp())
        {
            tipLabel.string = "恭喜钓到一条鱼"
        }
        else
        {
            tipLabel.string = "鱼逃走了"
        }
        tipLabel.fontSize = 40
        tipLabel.node.setPosition(368,206)
        // let move = cc.moveTo(1,cc.v2(368,300))
        // let destroy = cc.callFunc(function(){
        //     tipLabel.node.removeFromParent(true)
        // })
        // let showTip = cc.sequence([move,destroy])
        // tipLabel.node.runAction(showTip)
        cc.tween(tipLabel.node)
            .to(1, { position: cc.v2(368, 300)})
            .call(()=>{
                tipLabel.node.removeFromParent(true)
            })
            .start()
        this.node.addChild(tipLabel.node)
        
    }

    swing_throw_handler()
    {
        if(this.dirction == ThrowDirction.FORWARD)
        {
            let cur_progress = this.throwSlider.progress
            if (cur_progress + this.handlerSped >= 1.0) {
                this.throwSlider.progress += this.handlerSped
                this.dirction = ThrowDirction.BACKWARD
            }
            else
            {
                this.throwSlider.progress += this.handlerSped
            }
        }
        else
        {
            let cur_progress = this.throwSlider.progress
            if (cur_progress - this.handlerSped <= 0.0){
                this.throwSlider.progress -= this.handlerSped
                this.dirction = ThrowDirction.FORWARD
            }
            else 
            {
                this.throwSlider.progress -= this.handlerSped
            }
        }
        
    }

    click_btn(evt:cc.Event)
    {
        switch (this.sceneStatus) 
        {
            case SceneStatus.Idling:
                this.click_prepare_throw(evt)
                break;
            case SceneStatus.Prepare:
                this.click_throw(evt)
                break;
            case SceneStatus.Fighting:
                this.click_pull_rob(evt)
                break;
        }
    }
    click_prepare_throw(evt:cc.Event)
    {
        cc.log("click_prepare_throw "+evt)
        this.throwSlider.progress = 0
        this.dirction = ThrowDirction.FORWARD

        this.changeSceneStatus(SceneStatus.Prepare)
    }

    click_throw(evt:cc.Event)
    {
        cc.log("click_throw " + evt)
        this.changeSceneStatus(SceneStatus.Throwing)
    }

    click_pull_rob(evt:cc.Event)
    {
        if(this.sceneStatus == SceneStatus.Fighting && this.fishObj != null && this.catObj != null)
        {
            this.catObj.AddToughNess(5)
            this.fishObj.BeAttacked(20)
        }
        else
        {
            this.changeSceneStatus(SceneStatus.Ending)
        }
    }
}
